package com.zzxy.spring5.factory;

import com.zzxy.spring5.aspect.MyAspect;
import com.zzxy.spring5.impl.IStudentService;
import com.zzxy.spring5.impl.StudentServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyBeanFactoty {
    public static IStudentService createStudentService(){
        StudentServiceImpl studentService=new StudentServiceImpl();
        MyAspect aspect=new MyAspect();
        IStudentService stuServiceProxy =(IStudentService) Proxy.newProxyInstance(MyBeanFactoty.class.getClassLoader(), studentService.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                aspect.before();
                Object obj = method.invoke(studentService, args);
                aspect.after();
                return obj;
            }
        });
        return stuServiceProxy;
    }
}
